import { defineStore } from 'pinia'
import axios from 'axios'

import type {
  IRecipe,
  IChef,
  ICollectItem,
  IMaterialItem,
  IGame,
  IEquipItem,
  ISkillItem,
} from '@/stores/types'

type State = {
  recipes: IRecipe[]
  chefs: IChef[]
  maps: ICollectItem[]
  materials: IMaterialItem[]
  equips: IEquipItem[]
  skills: ISkillItem[]
}

export const useGameStore = defineStore('game', {
  state: (): State => ({
    chefs: [],
    recipes: [],
    maps: [],
    materials: [],
    equips: [],
    skills: [],
  }),
  getters: {
    chefsMap: (state) => new Map(state.chefs.map((m) => [m.chefId, m])),
    recipesMap: (state) => new Map(state.recipes.map((m) => [m.recipeId, m])),
    equipsMap: (state) => new Map(state.equips.map((m) => [m.equipId, m])),
    skillsMap: (state) => new Map(state.skills.map((m) => [m.skillId, m])),
    materialsMap: (state) => new Map(state.materials.map((m) => [m.materialId, m])),
  },
  actions: {
    async loadData() {
      try {
        const { data: gameData } = await axios.get<IGame>('./data.min.json')

        this.chefs = gameData.chefs
        this.recipes = gameData.recipes
        this.maps = gameData.maps
        this.materials = gameData.materials
        this.equips = gameData.equips
        this.skills = gameData.skills
      } catch (e) {
        console.error(e)
      }
    },
  },
})
